package com.zhongshengnetwork.rightbe.dbservice;

import com.zhongshengnetwork.rightbe.CustomApplication;
import com.zhongshengnetwork.rightbe.common.CommonUtils;
import com.zhongshengnetwork.rightbe.dbmodel.GroupUserdbModel;
import com.zhongshengnetwork.rightbe.dbmodel.SyncInfodbModel;

import org.xutils.DbManager;
import org.xutils.ex.DbException;
import org.xutils.x;

import java.util.List;

/**
 * Created by zhongsheng-luojianlan on 16/11/9.
 */

public class SyncInfodbService {
    //插入或者更新数据
    public static boolean saveOrUpdate(SyncInfodbModel model){
        DbManager db = x.getDb(CustomApplication.daoConfig);
        try {
            if (CommonUtils.isEmpty(model.getAddresstime())){
                model.setAddresstime("0");
            }
            db.saveOrUpdate(model);
            return true;
        } catch (DbException e) {
            e.printStackTrace();
            return false;
        }
    }

    //设置好友同步时间
    public static boolean setFriendTime(String friendtime,String userid){
        DbManager db = x.getDb(CustomApplication.daoConfig);
        try {
            SyncInfodbModel model = db.findById(SyncInfodbModel.class,userid);
            if (model != null){
                model.setFriendtime(friendtime);
                db.saveOrUpdate(model);
            }
            return true;
        } catch (DbException e) {
            e.printStackTrace();
            return false;
        }
    }

    //获取好友同步时间
    public static String getFriendTime(String userid){
        DbManager db = x.getDb(CustomApplication.daoConfig);
        try {
            if (db.findById(SyncInfodbModel.class,userid) != null)
              return db.findById(SyncInfodbModel.class,userid).getFriendtime();
            return "0";
        } catch (DbException e) {
            e.printStackTrace();
            return "0";
        }
    }

    //设置群同步时间
    public static boolean setGroupTime(String grouptime,String userid){
        DbManager db = x.getDb(CustomApplication.daoConfig);
        try {
            SyncInfodbModel model = db.findById(SyncInfodbModel.class,userid);
            if (model != null){
                model.setGrouptime(grouptime);
                db.saveOrUpdate(model);
            }
            return true;
        } catch (DbException e) {
            e.printStackTrace();
            return false;
        }
    }

    //获取群同步时间
    public static String getGroupTime(String userid){
        DbManager db = x.getDb(CustomApplication.daoConfig);
        try {
            if (db.findById(SyncInfodbModel.class,userid) != null)
               return db.findById(SyncInfodbModel.class,userid).getGrouptime();
            return "0";
        } catch (DbException e) {
            e.printStackTrace();
            return "0";
        }
    }

    //设置地址同步时间
    public static boolean setAddressTime(String addresstime,String userid){
        DbManager db = x.getDb(CustomApplication.daoConfig);
        try {
            SyncInfodbModel model = db.findById(SyncInfodbModel.class,userid);
            if (model != null) {
                model.setAddresstime(addresstime);
                db.saveOrUpdate(model);
            }
            return true;
        } catch (DbException e) {
            e.printStackTrace();
            return false;
        }
    }

    //获取地址同步时间
    public static String getAddressTime(String userid){
        DbManager db = x.getDb(CustomApplication.daoConfig);
        try {
            if (db.findById(SyncInfodbModel.class,userid) != null)
               return db.findById(SyncInfodbModel.class,userid).getAddresstime();
            return "0";
        } catch (DbException e) {
            e.printStackTrace();
            return "0";
        }
    }

    //设置使用时间
    public static boolean setTime(long time,String userid){
        DbManager db = x.getDb(CustomApplication.daoConfig);
        try {
            SyncInfodbModel model = db.findById(SyncInfodbModel.class,userid);
            if (model != null){
                model.setTime(time);
                db.saveOrUpdate(model);
            }
            return true;
        } catch (DbException e) {
            e.printStackTrace();
            return false;
        }
    }

    //获取使用时间
    public static long getTime(String userid){
        DbManager db = x.getDb(CustomApplication.daoConfig);
        try {
            if (db.findById(SyncInfodbModel.class,userid) != null)
              return db.findById(SyncInfodbModel.class,userid).getTime();
            return 0;
        } catch (DbException e) {
            e.printStackTrace();
            return 0;
        }
    }


    //删除数据
    public static boolean deleteModel(){
        DbManager db = x.getDb(CustomApplication.daoConfig);
        try {
            db.delete(SyncInfodbModel.class);
            return true;
        } catch (DbException e) {
            e.printStackTrace();
            return false;
        }

    }
}
